﻿package src.flip{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import src.events.*;
	import src.utils.*;
	import src.caurina.transitions.*;
	public final class Nav extends Sprite {
		
		private var flip:Flip = Flip.flip;
		private var config:Object = Flip.config;
		//
		private var rect:Rectangle;
		private var offsetX:Number = 0;
		private var offsetY:Number = 0;
		private var showL:Boolean;
		private var showR:Boolean;
		private var srcL:String;
		private var srcR:String;
		private var dragX:Number = 0;
		private var dragY:Number = 0;
		private var maskW:Number;
		private var maskH:Number;
		private var mainW:Number;
		private var mainH:Number = 100;
		//
		private var nav_mask:Sprite = new Sprite();
		private var nav_main:Sprite = new Sprite();
		private var nav_seek:Sprite = new Sprite();
		private var draging:Boolean = false;
		//
		public function Nav():void {
			this.visible = false;
			//
			nav_head.addEventListener(MouseEvent.MOUSE_DOWN, headDownHandler);
			flip.stage.addEventListener(MouseEvent.MOUSE_UP, headUpHandler);
			nav_button.addEventListener(MouseEvent.CLICK, buttonHandler);
			//
			nav_body.addChild(nav_main);
			nav_body.addChild(nav_seek);
			nav_body.addChild(nav_mask);
			nav_seek.mask = nav_mask;
			//
			nav_seek.addEventListener(MouseEvent.MOUSE_MOVE, seekMoveHandler);
			nav_seek.addEventListener(MouseEvent.MOUSE_DOWN, seekDownHandler);
			flip.stage.addEventListener(MouseEvent.MOUSE_UP, seekUpHandler);
			//
			flip.addEventListener(FlipEvent.RESIZE, resizeHandler);
			flip.addEventListener(FlipEvent.ZOOMER_LOAD, loadHandler);
			flip.addEventListener(FlipEvent.ZOOMER_SHOW, showHandler);
			flip.addEventListener(FlipEvent.ZOOMER_HIDE, hideHandler);
			flip.addEventListener(FlipEvent.ZOOMER_SEEK, seekHandler);
			resizeHandler();
		}
		//==============================================================================
		private function buttonHandler(e:MouseEvent):void {
			nav_body.visible = nav_back.visible = !nav_body.visible;
		}
		private function headDownHandler(e:MouseEvent):void {
			this.startDrag();
		}
		private function headUpHandler(e:MouseEvent):void {
			this.stopDrag();
		}
		private function seekDownHandler(e:MouseEvent):void {
			draging = true;
			dragX = nav_seek.mouseX;
			dragY = nav_seek.mouseY;
		}
		private function seekUpHandler(e:MouseEvent):void {
			draging = false;
		}
		private function seekMoveHandler(e:MouseEvent):void {
			if (draging) {
				var zx:Number = nav_body.mouseX - dragX;
				var zy:Number = nav_body.mouseY - dragY;
				//
				var minX:Number = 0;
				var maxX:Number = mainW - maskW;
				var minY:Number = 0;
				var maxY:Number = mainH - maskH;
				nav_seek.x = Flip.fitNumber(zx, minX, maxX);
				nav_seek.y = Flip.fitNumber(zy, minY, maxY);
				zoomerHandler();
			}
		}
		private function zoomerHandler():void {
			var sx:Number = nav_seek.x / (mainW + 2);
			var sy:Number = nav_seek.y / (mainH + 2);
			Flip.sendEvent(FlipEvent.NAV_SEEK, {sx:sx, sy:sy});
		}
		//==============================================================================
		private function loadHandler(e:FlipEvent):void {
			var dat:Object = e.data;
			//return;
			showL = dat.showL;
			showR = dat.showR;
			srcL = dat.srcL;
			srcR = dat.srcR;
			//trace("showL:" + showL + "|showR:" + showR + "|srcL:" + srcL + "|srcR:" + srcR);
			//
			Flip.clearPages(nav_main);
			//
			var pageL:Page;
			var pageR:Page;
			mainW = Math.round(config['page_width'] / config['page_height'] * mainH);
			if (!showL && showR) {
				pageR = new Page(srcR);
				pageR.load();
				pageR.width = mainW;
				pageR.height = mainH;
				nav_main.addChild(pageR);
			} else if (showL && !showR) {
				pageL = new Page(srcL);
				pageL.load();
				pageL.width = mainW;
				pageL.height = mainH;
				nav_main.addChild(pageL);
			} else {
				pageL = new Page(srcL);
				pageL.load();
				pageR = new Page(srcR);
				pageR.load();
				pageL.width = pageR.width = pageR.x = mainW;
				pageL.height = pageR.height = mainH;
				nav_main.addChild(pageL);
				nav_main.addChild(pageR);
				mainW = mainW * 2;
			}
			//
			nav_head.width = nav_back.width = mainW + 4;
			nav_button.x = mainW - 10;
			//
			Flip.drawRect(nav_mask, new Rectangle(0, 0, mainW, mainH));
			//
			layout();
		}
		
		private function showHandler(e:FlipEvent):void {
			this.visible = true;
			var dat:Object = e.data;
			offsetX = dat.offsetX;
			offsetY = dat.offsetY;
			//trace("offsetX:" + offsetX + "|offsetY:" + offsetY);
			var scale:Number = config['zoom'];
			maskW = Math.round(mainW / scale);
			maskH = Math.round(mainH / scale);
			nav_seek.buttonMode = true;
			with (nav_seek.graphics) {
				clear();
				beginFill(0x000000, 0.2);
				drawRect(- mainW, - mainH, mainW * 2 + maskW, mainH * 2 + maskH);
				drawRect(0, 0, maskW, maskH);
				endFill();
				beginFill(0xffffff, 0);
				lineStyle(1, 0xF8A701);
				drawRect(0, 0, maskW, maskH);
				endFill();
			}
		}
		
		private function seekHandler(e:FlipEvent):void {
			var sx:Number = e.data.sx;
			var sy:Number = e.data.sy;
			var scale:Number = config['zoom'];
			var tx:Number = -sx * mainW;
			var ty:Number = -sy * mainH;
			nav_seek.x = Math.ceil(tx);
			nav_seek.y = Math.ceil(ty);
		}
		
		private function hideHandler(e:FlipEvent):void {
			this.visible = false;
		}
		
		//=======================================================================================================
		private function resizeHandler(e:FlipEvent = null):void {
			this.x = config['width'] - mainW - 4;
			this.y = config['height'] - mainH - 17;
		}
		
		private function layout():void {
			this.x = Flip.fitNumber(this.x, 0, config['width'] - mainW - 4);
			this.y = Flip.fitNumber(this.y, 0, config['height'] - mainH - 17);
		}

		private function drawRect(clip:Sprite, rect:Rectangle):void {
			clip.graphics.clear();
			clip.graphics.beginFill(0xffffff, 1);
			clip.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
			clip.graphics.endFill();
		}
		
	}
}